﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text;

namespace Lifang.Common
{
    public class DataPage : INotifyPropertyChanged
    {
        /// <summary>分页页大小0.不分页</summary>
        public const string PageSizeField = "PageSize";
        /// <summary>显示页编码</summary>
        public const string PageIndexField = "PageIndex";
        /// <summary>返回总信息量</summary>
        public const string RowCountField = "RowCount";
        /// <summary>返回总页数 </summary>
        public const string PageCountField = "PageCount";
        /// <summary>排序字段</summary>
        public const string OrderFieldName = "OrderField";
        private static List<string> _Columns = new List<string>
        {
            "PageSize",
            "PageIndex",
            "RowCount",
            "PageCount",
            "OrderField"
        };
        private int _pageSize = 20;
        private int _pageIndex = 1;
        private long _rowCount;
        private int _pageCount = 1;
        private int _rowSkip;
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>对象属性名集合</summary>
        public static List<string> Columns
        {
            get
            {
                return DataPage._Columns;
            }
        }
        /// <summary>查寻是要获的字段,号分隔</summary>
        public string ShowFieldsOnly
        {
            get;
            set;
        }
        /// <summary>分页页大小[设置0时不分页]</summary>
        [DataMember]
        public int PageSize
        {
            get
            {
                return this._pageSize;
            }
            set
            {
                this._pageSize = value;
                if (this._pageSize != 0)
                {
                    if (this._rowCount % (long)this._pageSize != 0L)
                    {
                        this._pageCount++;
                    }
                    else
                    {
                        this._pageCount = (int)(this._rowCount / (long)this._pageSize);
                    }
                }
                this.OnPropertyChanged("PageSize");
            }
        }
        /// <summary>显示页编码 </summary>
        [DataMember]
        public int PageIndex
        {
            get
            {
                if (this._pageIndex <= 0)
                {
                    this._pageIndex = 1;
                }
                return this._pageIndex;
            }
            set
            {
                this._pageIndex = value;
                this.OnPropertyChanged("PageIndex");
            }
        }
        /// <summary>返回总信息量</summary>
        [DataMember]
        public long RowCount
        {
            get
            {
                return this._rowCount;
            }
            set
            {
                this._rowCount = value;
                if (this._pageSize != 0)
                {
                    this._pageCount = (int)(this._rowCount / (long)this._pageSize);
                    if (this._rowCount % (long)this._pageSize != 0L)
                    {
                        this._pageCount++;
                    }
                    if (this._pageIndex > this._pageCount)
                    {
                        this._pageIndex = this._pageCount;
                    }
                }
                this.OnPropertyChanged("RowCount");
            }
        }
        /// <summary>返回总页数</summary>
        [DataMember]
        public int PageCount
        {
            get
            {
                return this._pageCount;
            }
            set
            {
                this._pageCount = value;
                this.OnPropertyChanged("PageCount");
            }
        }
        /// <summary>返回跳过的记录数</summary>
        [DataMember]
        public int RowSkip
        {
            get
            {
                this._rowSkip = (this.PageIndex - 1) * this.PageSize;
                return this._rowSkip;
            }
            set
            {
                this._rowSkip = value;
            }
        }
        /// <summary>排序字段</summary>
        [DataMember]
        public string OrderField
        {
            get;
            set;
        }
        /// <summary>执行时间 </summary>
        [DataMember]
        public double ExeTime
        {
            get;
            set;
        }
        [DataMember]
        public string Msg
        {
            get;
            set;
        }
        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
